#include "offspinbox.h"
#include "dm/dmdef.h"


OffSpinBox::OffSpinBox(const Off &type, const QString &suffix, QWidget *parent)
    : ASpinBox{parent}
    , m_type{type}
    , m_suffix(suffix)
{

}

QString OffSpinBox::textFromValue(int value) const
{
    if ((value == minimum() && m_type == Min) || (value == maximum() && m_type == Max)) {
        return tr(LABEL_OFF);
    }
    return QString("%1%2").arg(value).arg(m_suffix);
}

int OffSpinBox::valueFromText(const QString &text) const
{
    if (text == tr(LABEL_OFF)) {
        if (m_type == Min) {
            return minimum();
        }
        else {
            return maximum();
        }
    }
    QRegularExpression regExp(QString("(-?\\d+)%1").arg(m_suffix));

    QRegularExpressionMatch match = regExp.match(text);
    if (match.hasMatch()) {  // 替代exactMatch()
        return match.captured(1).toInt();  // 替代cap(1)
    }
    return text.toInt();
}
